let app = getApp()
Page({
  data: {
    isSubscribe: false,
    ossImg: `${app.globalData.imgUrl}/applet_web_images/new_image`,
    zm_user_id: '' // 支付宝ID
  },

  onLoad() {
    this.getSubscribeState()
    let app = getApp()
    app.globalData.exclusivePrice = '' // tabBar栏都要清除专享价
  },
  // 加群
  addGroup () {
    my.ap.openURL({
      url: 
      'alipays://platformapi/startapp?appId=68687451&url=%2Fwww%2Fbc-join.html%3Foid%3D2023020316330500021149%26businessId%3D2023020316330500021149%26businessType%3D18%26source%3DOTHERS'
    })
  },
  returnHome() {
    my.switchTab({
      url: '/page/rent/rent'
    })
  },
  // 获取手机号
  handleGetPhone() {
    const app = getApp()
    my.getPhoneNumber({
      success: (res) => {
        let encryptedData = JSON.parse(res.response).response;
        const { testUrl } = app.globalData
        const { zm_user_id } = this.data
        my.request({
          url: `${testUrl}/Api/order/phoneDecrypt`,
          method: 'POST',
          dataType: 'json',
          data: {
            phone_str: encryptedData,
            zm_user_id
          },
          success: (res) => {
            const {
              phone
            } = res.data.data
            app.globalData.userPhone = phone
          // 获取消息通知
          my.requestSubscribeMessage({
            entityIds: [
              '6f0f193e6ab24fd8947c21a1e9a7fe87'
            ],
            success: res => {
              const { behavior } = res
              if(behavior === 'subscribe') {
                my.request({
                  url: `${testUrl}/api/event/eventSubscribe`,
                  data: {
                    zm_user_id,
                    template_id: '6f0f193e6ab24fd8947c21a1e9a7fe87'
                  },
                  method: 'post',
                  dataType: 'json',
                  success: ({data: {code, msg: title}}) => {
                    if(code !== 1) { // 已订阅
                      my.alert({ 
                        title,
                        buttonText: '我知道了',
                      })
                      return
                    }
                    // 刷新已订阅
                   this.getSubscribeState()
                  }
                })
              }
            },
            fail: err => {
              const { errorMessage  } = err
              my.showToast({
                type: 'fail',
                content: errorMessage ,
                duration: 1000
              })
            }
          })
          }
        });
      },
      fail: (res) => {
        console.log(1, res)
      },
    })
  },
  alreadySubscribe() {
    my.alert({ 
      title: '当前活动已订阅，请勿重复订阅!',
      buttonText: '我知道了',
    })
  },
  // 获取订阅状态
  getSubscribeState() {
    const app = getApp();
    const { testUrl } = app.globalData
     app.getAuth_base((zm_user_id) => {
      this.data.zm_user_id = zm_user_id
      my.request({
        url: `${testUrl}/api/event/eventStatus`,
        data: {
          zm_user_id,
          template_id: '6f0f193e6ab24fd8947c21a1e9a7fe87'
        },
        method: 'post',
        success: (res) => {
          const { data: { code } } = res
          if(code === 1)  {
            this.setData({
              isSubscribe: true
            })
          }
        }
      })
     })
  } 
})
